package com.salt.modules.sys.service;

import com.google.common.collect.Lists;
import com.salt.common.config.properties.SaltProperites;
import com.salt.common.service.TreeService;
import com.salt.modules.sys.entity.Office;
import com.salt.modules.sys.mapper.CompanyMapper;
import com.salt.modules.sys.mapper.OfficeMapper;
import com.salt.modules.sys.mapper.UserMapper;
import com.salt.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 机构Service
 * @author salt
 * @version 2017-05-16
 */
@Service
@Transactional(readOnly = true)
public class OfficeService extends TreeService<OfficeMapper, Office> {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CompanyMapper companyMapper;

	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}
	
	@Transactional(readOnly = true)
	public List<Office> findList(Office office){
		office.setParentIds(office.getParentIds()+"%");
		return mapper.findByParentIdsLike(office);
	}
	
	@Transactional(readOnly = true)
	public Office getByCode(String code){
		return mapper.getByCode(code);
	}
	
	public List<Office> getChildren(String parentId){
		return mapper.getChildren(parentId);
	}

	public List<Office> getUserableChildren(String parentId) {
		List<Office> children = mapper.getChildren(parentId);
		ArrayList<Office> userableChildrenList = Lists.newArrayList();
		for (Office office:children) {
			if (SaltProperites.YES.equals(office.getUseable())) {
				userableChildrenList.add(office);
			}
		}
		return userableChildrenList;
	}
	
	@Transactional(readOnly = false)
	public void save(Office office) {
		super.save(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(Office office) {
		// TODO 逻辑删除注册用户、公司信息
		/*if (Objects.equals(office.getType(), "1")) {
			// 逻辑删除注册用户
			User user = new User();
			user.setCompany(office);
			List<User> userList = userMapper.findListByOffice(user);
			if (userList != null && userList.get(0).getCompany1() != null) {
				Company companyInfo = userList.get(0).getCompany1();
				user.setCompany1(companyInfo);
				user.setDelFlag("1");
				userMapper.deleteByCompanyLogic(user);
				// 逻辑删除
				companyInfo.setDelFlag("1");
				companyMapper.deleteByLogic(companyInfo);
			}
		}*/
		super.delete(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}

	public List<Office> findByIds(@Param("ids") List<String> ids){
		return mapper.findByIds(ids);
	}

	public List<Office> findFuncLocationOfficeList(Office office) {
		return mapper.findList(office);
	}

	public List<Office> treeDataLocationIds(Office office) {
		return mapper.findByParentIdsLike(office);
	}
	
}
